package com.dd.cloud.user.mq;

import com.dd.cloud.common.constants.IntegralConstants;
import com.dd.cloud.common.interceptor.UserInterceptor;
import com.dd.cloud.common.vo.LoginInfo;
import com.dd.cloud.user.service.chknowl.IChknowlBooksPresentService;
import com.dd.cloud.user.service.consule.IConsultSubscribeService;
import com.dd.cloud.user.service.doctor.*;
import com.dd.cloud.user.service.log.ILogErrorService;
import com.dd.cloud.user.service.news.IAppPromotionNewsService;
import com.dd.cloud.user.service.news.INewsInfoService;
import com.dd.cloud.user.service.order.IOrderPayService;
import com.dd.cloud.user.service.plat.TemplateMessAgeService;
import com.dd.cloud.user.service.pres.IPresPrescriptionService;
import com.dd.cloud.user.vo.DelayMqVo;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * rabbitmq延迟队列消息消费者(定时器任务)
 *
 * @author vodbook
 */
@Component
@Slf4j
public class DelayMQMessage {
    @Autowired
    private IOrderPayService orderPayService;
    @Autowired
    private IPresPrescriptionService presPrescriptionService;
    @Autowired
    private IConsultSubscribeService consultSubscribeService;
    @Autowired
    private IDoctorOrderIncomeDetailService doctorOrderIncomeDetailService;
    @Autowired
    private ILogErrorService logErrorService;
    @Autowired
    private TemplateMessAgeService templateMessAgeService;
    @Autowired
    private INewsInfoService newsInfoService;
    @Autowired
    private IAppPromotionNewsService appPromotionNewsService;
    @Autowired
    private IDoctorIntegralService doctorIntegralService;
    @Autowired
    private IChknowlBooksPresentService chknowlBooksPresentService;
    @Autowired
    private IDoctorSignService doctorSignService;
    @Autowired
    private IDoctorTaskRecordService doctorTaskRecordService;
    @Autowired
    private IDoctorUserService doctorUserService;

    @RabbitListener(queues = MQConfig.DELAY_QUEUE)
    public void delay(DelayMqVo<?> delayMqVo, Channel channel, Message message) throws IOException {
        try {
            log.info("===============接收延迟队列接收消息====================");
            log.info("----->delayMqVo:{},message:{}", delayMqVo, message);
            //处方过期定时器
            if (delayMqVo.getType() == DelayMqVo.DelayMqVoType.ORDER_CANCEL) {
                LoginInfo loginInfo = new LoginInfo();
                loginInfo.setType(LoginInfo.UserType.ADMIN);
                UserInterceptor.getLoginUserInfo().set(loginInfo);
                String orderNo = (String) delayMqVo.getT();
                orderPayService.cancelOrder(orderNo, LoginInfo.UserType.ADMIN);
            } else if (delayMqVo.getType() == DelayMqVo.DelayMqVoType.PRES_COMPLETE) {
                String tencode = (String) delayMqVo.getT();
                presPrescriptionService.presAutoComplete(tencode);
            } else if (delayMqVo.getType() == DelayMqVo.DelayMqVoType.PRES_REVIEW_TIMEOUT) {
                String tencode = (String) delayMqVo.getT();
                presPrescriptionService.presReviewTimeout(tencode);
            } else if (delayMqVo.getType() == DelayMqVo.DelayMqVoType.CONSULT_TIMEOUT) {
                //预约订单过期定时器
                Integer id = Integer.valueOf((String) delayMqVo.getT());
                consultSubscribeService.consultOrderTimeout(id);
            } else if (delayMqVo.getType() == DelayMqVo.DelayMqVoType.CONSULT_AUTO_COMPLETE) {
                log.info("进入预约订单自动结束定时器");
                //预约订单自动结束定时器
                Integer id = Integer.valueOf((String) delayMqVo.getT());
                consultSubscribeService.consultAutoComplete(id);
            } else if (delayMqVo.getType() == DelayMqVo.DelayMqVoType.CONSULT_PATIENT_NOT_REPLY) {
                //预约医生接诊患者未回复定时器
                Integer id = Integer.valueOf((String) delayMqVo.getT());
                consultSubscribeService.consultPatientNotReply(id);
            } else if (delayMqVo.getType() == DelayMqVo.DelayMqVoType.CONSULT_ORDER_SETTING) {
                //问诊订单分账设置
                String[] temp = ((String) delayMqVo.getT()).split("-");
                if (temp.length == 2) {
                    doctorOrderIncomeDetailService.doctorOrderIncomeEntrySuc(temp[0], Integer.valueOf(temp[1]));
                }
            } else if (delayMqVo.getType() == DelayMqVo.DelayMqVoType.INQUIRY_ORDER_REMIND) {
                String[] temp = ((String) delayMqVo.getT()).split("-");
                if (temp.length == 2) {
                    log.info("RabbitMQ发送问诊订单待缴费模板消息");
                    templateMessAgeService.sendOrderNotPayTemplateMessage(temp[0], temp[1]);
                    log.info("RabbitMQ发送问诊待缴费系统消息");
                    newsInfoService.sendOrderNotPayMessage(temp[0], temp[1]);
                }
            } else if (delayMqVo.getType() == DelayMqVo.DelayMqVoType.PRE_ORDER_REMIND) {
                String[] temp = ((String) delayMqVo.getT()).split("-");
                if (temp.length == 2) {
                    log.info("RabbitMQ发送处方订单待缴费模板消息");
                    templateMessAgeService.sendPresOrderNotPayTemplateMessage(temp[0], temp[1]);
                    log.info("RabbitMQ发送处方订单待缴费系统消息");
                    newsInfoService.sendPresOrderNotPayMessage(temp[0], temp[1]);
                }
            } else if (delayMqVo.getType() == DelayMqVo.DelayMqVoType.APP_UNIPUSH_MSG) {
                log.info("进入发送app消息定时器");
                //发送app消息定时器
                Integer id = Integer.valueOf((String) delayMqVo.getT());
                appPromotionNewsService.reSendAppPromotionNews(id, 2);
            } else if (delayMqVo.getType() == DelayMqVo.DelayMqVoType.DOCTOR_INTEGRAL_MSG) {
                log.info("进入积分延时");
                Integer id = (Integer) delayMqVo.getT();
                doctorIntegralService.updateStatus(id, IntegralConstants.status.LOSE);
            } else if (delayMqVo.getType() == DelayMqVo.DelayMqVoType.BOOK_TRANSITION) {
                log.info("获取书籍目录");
                Integer bookId = (Integer) delayMqVo.getT();
                chknowlBooksPresentService.getBookTransition(bookId);
            } else if (delayMqVo.getType() == DelayMqVo.DelayMqVoType.DOCTOR_TASK) {
                log.info("进入医生任务列表");
                Integer taskId = (Integer) delayMqVo.getT();
                doctorTaskRecordService.updateTaskStatus(taskId);
            } else if (delayMqVo.getType() == DelayMqVo.DelayMqVoType.DOCTOR_NO_AUTH) {
                log.info("进入医生注册48小时未认证任务");
                Integer doctorId = (Integer) delayMqVo.getT();
                doctorUserService.doctorUserAuthSms(doctorId);
            } else {
                throw new IllegalStateException("Unexpected value: " + delayMqVo.getType());
            }
            //应答，消息已处理
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            e.printStackTrace();
            if (message.getMessageProperties().getRedelivered()) {
                log.error("延迟消息已重复处理失败,拒绝再次接收...");
                // 拒绝消息
                channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
            } else {
                e.printStackTrace();
                log.error("延迟消息即将再次返回队列处理...");
            }
            logErrorService.presBackLog(e.getMessage(), "延时队列消费者异常", "params=" + delayMqVo);
        }
    }
}
